package com.gitee.feizns.explore.data_structure.linked_list.singly.swap;

/**
 * @author feizns
 * @since 2019/12/10
 */
public class SwapPairs {

    public static void main(String[] args) {
        ListNode node = swapPairs(create());
        node.print();
    }

    public static ListNode swapPairs(ListNode head) {
        if ( head != null && head.next != null ) {
            ListNode next = head.next;
            head.next = swapPairs(head.next.next);
            next.next = head;
            return next;
        }
        return head;
    }

    private static ListNode create() {
        ListNode node = new ListNode(1);
        node.next = new ListNode(2);
        node.next.next = new ListNode(3);
        node.next.next.next = new ListNode(4);
        return node;
    }

}
